﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.CompilerServices;
using AutoR.Models;
using AutoR.Services;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Navigation;
using Prism.Services;

namespace AutoR.ViewModels.Tasks
{
	// Token: 0x02000088 RID: 136
	public class TacticsSelectViewModel : BindableBase, INavigationAware, INavigatedAware, INavigatingAware
	{
		// Token: 0x1700008F RID: 143
		// (get) Token: 0x0600025E RID: 606 RVA: 0x0004620D File Offset: 0x0004440D
		private INavigationService NavigationService { get; }

		// Token: 0x17000090 RID: 144
		// (get) Token: 0x0600025F RID: 607 RVA: 0x00046215 File Offset: 0x00044415
		private IPageDialogService PageDialogService { get; }

		// Token: 0x17000091 RID: 145
		// (get) Token: 0x06000260 RID: 608 RVA: 0x000408F6 File Offset: 0x0003EAF6
		public GameData Game
		{
			get
			{
				return Runtime.Game;
			}
		}

		// Token: 0x17000092 RID: 146
		// (get) Token: 0x06000261 RID: 609 RVA: 0x0004621D File Offset: 0x0004441D
		public DelegateCommand<TacticSelectItem> ChangeCommand { get; }

		// Token: 0x17000093 RID: 147
		// (get) Token: 0x06000262 RID: 610 RVA: 0x00046225 File Offset: 0x00044425
		public DelegateCommand ClearCommand { get; }

		// Token: 0x17000094 RID: 148
		// (get) Token: 0x06000263 RID: 611 RVA: 0x0004622D File Offset: 0x0004442D
		// (set) Token: 0x06000264 RID: 612 RVA: 0x00046238 File Offset: 0x00044438
		public IList<string> sourceList
		{
			[CompilerGenerated]
			get
			{
				return this.<sourceList>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (object.Equals(this.<sourceList>k__BackingField, value))
				{
					return;
				}
				this.<sourceList>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.sourceList);
			}
		}

		// Token: 0x17000095 RID: 149
		// (get) Token: 0x06000265 RID: 613 RVA: 0x00046268 File Offset: 0x00044468
		// (set) Token: 0x06000266 RID: 614 RVA: 0x00046270 File Offset: 0x00044470
		public ObservableCollection<TacticSelectItem> List
		{
			[CompilerGenerated]
			get
			{
				return this.<List>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (object.Equals(this.<List>k__BackingField, value))
				{
					return;
				}
				this.<List>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.List);
			}
		}

		// Token: 0x06000267 RID: 615 RVA: 0x000462A0 File Offset: 0x000444A0
		public TacticsSelectViewModel(INavigationService navigationService, IPageDialogService pageDialogService)
		{
			this.<List>k__BackingField = new ObservableCollection<TacticSelectItem>();
			base..ctor();
			this.NavigationService = navigationService;
			this.PageDialogService = pageDialogService;
			this.ChangeCommand = new DelegateCommand<TacticSelectItem>(new Action<TacticSelectItem>(this.OnChangeCommandExecuted));
			this.ClearCommand = new DelegateCommand(delegate()
			{
				this.sourceList.Clear();
				this.GenList();
			});
		}

		// Token: 0x06000268 RID: 616 RVA: 0x0004067F File Offset: 0x0003E87F
		public void OnNavigatedFrom(INavigationParameters parameters)
		{
		}

		// Token: 0x06000269 RID: 617 RVA: 0x000462FA File Offset: 0x000444FA
		public void OnNavigatedTo(INavigationParameters parameters)
		{
			if (parameters.ContainsKey("list"))
			{
				this.sourceList = (IList<string>)parameters["list"];
			}
			this.GenList();
		}

		// Token: 0x0600026A RID: 618 RVA: 0x0004067F File Offset: 0x0003E87F
		public void OnNavigatingTo(INavigationParameters parameters)
		{
		}

		// Token: 0x0600026B RID: 619 RVA: 0x00046328 File Offset: 0x00044528
		public void GenList()
		{
			if (this.sourceList == null)
			{
				return;
			}
			List<TacticSelectItem> list = new List<TacticSelectItem>();
			using (List<Tactic>.Enumerator enumerator = this.Game.SchoolTactic.ToList<Tactic>().GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					Tactic tac = enumerator.Current;
					list.Add(new TacticSelectItem
					{
						Tactic = tac,
						Selected = this.sourceList.Any((string i) => int.Parse(i.Split(new char[]
						{
							'|'
						})[0]) == tac.boat_id && int.Parse(i.Split(new char[]
						{
							'|'
						})[1]) == tac.tactics_id)
					});
				}
			}
			list = (from i in list
			orderby !i.Selected
			select i).ToList<TacticSelectItem>();
			this.List = new ObservableCollection<TacticSelectItem>(list);
		}

		// Token: 0x0600026C RID: 620 RVA: 0x00046404 File Offset: 0x00044604
		private void OnChangeCommandExecuted(TacticSelectItem item)
		{
			if (item != null)
			{
				if (item.Selected)
				{
					if (!this.sourceList.Any((string i) => int.Parse(i.Split(new char[]
					{
						'|'
					})[0]) == item.Tactic.boat_id && int.Parse(i.Split(new char[]
					{
						'|'
					})[1]) == item.Tactic.tactics_id))
					{
						this.sourceList.Add(string.Format("{0}|{1}", item.Tactic.boat_id, item.Tactic.tactics_id));
						return;
					}
				}
				else if (this.sourceList.Any((string i) => int.Parse(i.Split(new char[]
				{
					'|'
				})[0]) == item.Tactic.boat_id && int.Parse(i.Split(new char[]
				{
					'|'
				})[1]) == item.Tactic.tactics_id))
				{
					this.sourceList.Remove(string.Format("{0}|{1}", item.Tactic.boat_id, item.Tactic.tactics_id));
				}
			}
		}
	}
}
